import { reactive, onMounted, defineComponent, toRefs, getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import stocktakingApi from '@/api/inwarehouse/stocktaking-api'
import warehouseApi from '@/api/inwarehouse/warehouse-api'
import goodsApi from '@/api/inwarehouse/goods-api'
import ZlrtTable from '@/components/zlrt-table/table.vue'
import ZlrtSearch from '@/components/zlrt-search/search.vue'
import TableContext from '@/components/zlrt-table/table-context'

export default defineComponent({
  components: {
    ZlrtTable,
    ZlrtSearch
  },
  setup() {
    const statusOptions = [
      { value: '0', label: '新增' },
      { value: '99', label: '已完成' },
      // { value: '100', label: '已同步' }
    ]

    const ctx = reactive(new TableContext(getCurrentInstance(), {
      title: '盘点单',
      api: stocktakingApi,
      columns: [
        // { prop: 'serialId', label: '单号', valueType: 'input' },
        { prop: 'locationName', label: '库位名称', valueType: 'input' },
        { prop: 'locationCode', label: '库位编码', valueType: 'input' },
        { prop: 'status', label: '状态', valueType: 'select', options: statusOptions, slot: true },
        { prop: 'createOn', label: '日期', slot: true },
        { prop: 'operation', label: '操作', slot: true }
      ],
      other: {
        formatDt(val) {
          if (!val) {
            return '-'
          }
          return val.substring(0, 10)
        },
        getStatusName(val: string) {
          const obj = statusOptions.find(item => {
            return item.value === val
          })
          if (obj) {
            return obj['label']
          }
          return '-'
        },
        handleWarehouse(val: string) {
          const obj = data.warehouseOptions.find(item => {
            return item.id === val
          })
          ctx.editData.warehouseName = obj['name']
        },
        addGoods() {
          if (!ctx.editData['orderGoodsList']) {
            ctx.editData['orderGoodsList'] = []
          }
          ctx.editData['orderGoodsList'].push({})
        },
        delGoods() {
          data.delGoodsList.map(dgl => {
            const index = ctx.editData['orderGoodsList'].findIndex(item => {
              return item.id === dgl.id
            })
            ctx.editData['orderGoodsList'].splice(index, 1)
          })
        },
        handleGoodsSelectionChange(val: any) {
          if (!ctx.editData['orderGoodsList']) {
            return
          }
          data.delGoodsList = val
        },
        handleGoodsChange(val: string) {
          const obj = data.goodsOptions.find(item => {
            return item.skuName === val
          })
          const index = ctx.editData['orderGoodsList'].findIndex(item => {
            return item.skuName === val
          })
          ctx.editData['orderGoodsList'].splice(index, 1, obj)
        }
      }
    }))

    const getWarehouseList = async () => {
      const params = { filter: { disabled: false } }
      const res = await warehouseApi.list(params)
      data.warehouseOptions = res.data.records
    }

    const getGoodsList = async (val: string) => {
      data.getGoodsLoading = true
      const params = { otherFilter: { skuName: val } }
      const res = await goodsApi.list(params)
      data.goodsOptions = res.data.records
      data.getGoodsLoading = false
    }


    const data = reactive({
      ctx,
      warehouseOptions: [],
      goodsOptions: [],
      getGoodsLoading: false,
      delGoodsList: [],
      getGoodsList
    })

    // init
    onMounted(() => {
      getWarehouseList()
      ctx.loadData()
    })

    return {
      ...toRefs(data)
    }
  }
})
